<?php

/*************************** 商品表 ***************************/
namespace app\admin\controller;
use app\common\logic\ProductLogic;
use think\Container;

class Product extends Common
{
    /**
     * 商品列表
     */
    public function index()
    {
        $Product = new ProductLogic;
        if (input('get.pd_name')) {
            $this->map[] = ['pd_name', 'like', "%" . trim(input('get.pd_name')) . "%"];
        }

        if (is_numeric(input('get.pd_status'))) {
            $this->map[] = ['pd_status', '=', input('get.pd_status')];
        }
        if (is_numeric(input('get.ca_id'))) {
            $id = input('get.ca_id');
            $this->map[] = ['ca_id', '=', $id];
        }
        $list = $Product->getList($this->map, $this->order, $this->size);

        $this->assign(array(
            'list' => $list,
        ));
        return $this->fetch();

    }

    /**
     * 修改商品
     */
    public function edit()
    {
        $editProduct = new ProductLogic;
        if (is_post()) {
            $data = input('post.');
            if (!array_key_exists('pd_pic',$data)){
                $this->error('请添加商品主图');
            }
            $rel = $editProduct->saveProduct($data,1);


            if ($rel['code'] = 1) {
                $this->success($rel['msg']);
            } else {
                $this->error($rel['msg']);
            }
            return $rel;
        }        
        $info = $editProduct->getOne(input('get.id'));
        $ghInfo = model('admin')
            ->where('ro_id',3)
            ->field('id,ad_tel,ad_name')
            ->select();
//        halt($info);
        $this->assign(array(
            'info' => $info,
            'ghInfo'=>$ghInfo
        ));
        return $this->fetch();
    }

    /**
     * 添加商品
     */
    public function add()
    {
        if (is_post()) {
            $data = input('post.');

            if (!array_key_exists('pd_pic',$data)){
                $this->error('请添加商品主图');
            }
            $addProduct = new ProductLogic;
            $rel = $addProduct->saveProduct($data);
            return $rel;           
        }
        $ghInfo = model('admin')
            ->where('ro_id',3)
            ->field('id,ad_tel,ad_name')
            ->select();

        $this->assign(array(
            'ghInfo'=>$ghInfo
        ));

        return $this->fetch();
    }

    //上传图片
    public function upload()
    {
        //$aaaa = input('post.');
        //halt($aaaa);
        $bb = Container::get('env')->get('ROOT_PATH');
        $file = request()->file('file');
        $info = $file->validate(['size' => '4096000'])
            ->move($bb . 'public/uploads/');
        if ($info) {
            $path = '/uploads/' . $info->getsavename();
            $path = str_replace("\\", "/", $path);
            return $data = array(
                'code' => 1,
                'msg' => '上传成功',
                'data' => $path,
            );
        } else {
            return $data = array(
                'msg' => $file->getError(),
                'code' => 0,
            );
        }
    }

    //列表修改状态
    public function change(){
        if (is_post()) {
            $rst = model('Product')->editInfo([input('post.key') => input('post.value')], ['id' => input('post.id')]);
            if ($rst) {
                return [
                    'code' => 1,
                    'msg' => '修改成功',
                    'data' => $rst,
                ];
            } 
        }
    }


    // 删除
    public function dodelete(){
        $every = new Every;
        $data = input('post.');
        $every->allDel($data);
    }


}